import Vue from 'vue'
import Router from 'vue-router'
import Main from '@/pages/main'
import store from "./store";
import {getUserDataFn} from '@/api'

Vue.use(Router)

export const asyncRoutes = [
    {
        path: '/data/group',
        name: 'group',
        component: resolve => require(['@/pages/main/group'], resolve),
        meta: {
            title: '分组管理'
        }
    },
    {
        path: '/data/vehicle',
        name: 'vehicle',
        component: resolve => require(['@/pages/main/vehicle'], resolve),
        meta: {
            title: '车辆管理'
        }
    },
    {
        path: '/data/vehicletype',
        name: 'vehicle',
        component: resolve => require(['@/pages/main/vehicletype'], resolve),
        meta: {
            title: '车型管理'
        }
    },
    {
        path: '/data/device',
        name: 'device',
        component: resolve => require(['@/pages/main/device'], resolve),
        meta: {
            title: '设备管理'
        }
    },
    {
        path: '/data/person',
        name: 'person',
        component: resolve => require(['@/pages/main/person'], resolve),
        meta: {
            title: '人员管理'
        },
    },
    {
        path: '/data/area',
        name: 'area',
        component: resolve => require(['@/pages/main/area'], resolve),
        meta: {
            title: '区域管理'
        }
    },
    {
        path: '/data/department',
        name: 'department',
        component: resolve => require(['@/pages/main/department'], resolve),
        meta: {
            title: '部门管理'
        }
    },
    {
        path: '/data/work',
        name: 'work',
        component: resolve => require(['@/pages/main/work'], resolve),
        meta: {
            title: '职务管理'
        }
    },
    {
        path: '/data/user/account',
        name: 'account',
        component: resolve => require(['@/pages/main/admin/account'], resolve),
        meta: {
            title: '账号管理'
        }
    },
    {
        path: '/data/user/role',
        name: 'role',
        component: resolve => require(['@/pages/main/admin/role'], resolve),
        meta: {
            title: '角色管理'
        }
    },
    {
        path: '/data/user/edit',
        name: 'edit',
        component: resolve => require(['@/pages/main/admin/role/edit'], resolve),
        meta: {
            title: '编辑角色'
        }
    },
    {
        path: '/data/user/menu',
        name: 'menu',
        component: resolve => require(['@/pages/main/admin/menu'], resolve),
        meta: {
            title: '菜单管理'
        }
    },
    {
        path: '/data/log',
        name: 'log',
        component: resolve => require(['@/pages/main/log'], resolve),
        meta: {
            title: '系统日志'
        }
    },
]
export const constantRoutes =
    [
    {
        path: '/',
        name: 'login',
        component: resolve => require(['@/pages/login'], resolve)
    },
    {
        path: '/main',
        name: 'Main',
        component: Main,
        redirect: '/home',
        children: [
            {
                path: '/home',
                name: 'Home',
                component: resolve => require(['@/pages/main/home'], resolve),
                meta: {
                    title: '系统首页',
                    affix: true
                }
            }
        ]
    }
]

const router = new Router({
    scrollBehavior: () => ({y: 0}),
    routes: [
        ...constantRoutes,
    ],
    // mode: 'history'
})


router.beforeEach((to, from, next) => {
    // console.log(to)
    // console.log(from)
    if (to.matched.length === 0) {
        from.name ? next({name: from.name}) : next('/')
    } else if (to.name !== 'login') {
        if (localStorage.token) {
            next()
            getUserDataFn().then(res => {
                store.state.user.userName = res.data
            })
        } else {
            localStorage.clear()
            next({path: '/'})
        }
    } else {
        next()
    }
})

export default router
